#!/usr/bin/perl

#    PerlTweets Twitter client
#    Copyright (C) 2009 Andrew Steinborn

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

use Net::Twitter;
use WWW::Shorten::Bitly;

my $nt = Net::Twitter->new(
    traits          => ['API::REST', 'OAuth'],
    consumer_key    => "n42QfyzybkP1tSn2iqkQ",
    consumer_secret => "o0IgHsYe8dUbHfvkHycvwDw2EFjCgaEdjUe8lXj4k",
    ssl             => 1,
);
unless ( $nt->authorized ) {
    # The client is not yet authorized: Do it now
    print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN#\n";

    my $pin = <STDIN>; # wait for input
    chomp $pin;

    my($access_token, $access_token_secret) = $nt->request_access_token(verifier => $pin);
    $nt->access_token($access_token);
    $nt->access_token_secret($access_token_secret);
}

# Everything's ready
print "Ready. Have fun!\n";

sub cmdline {
    print ">";
    my $code = <STDIN>;
    chomp $code;
    if ($code eq "tweet") {
       print "New status: ";
       my $tweet = <STDIN>;
       chomp $tweet;
       if ($tweet eq "") {
             $tweet = "Chillin'";
       }
       $nt->update({ status => $tweet });
       print "Tweet sent\n";
    }
    if ($code eq "shorturl") {
       print "URL to shorten: ";
       my $url = <STDIN>;
       chomp $url;
       if ($url eq "") {
             $url = "http://www.google.com/";
       }
       my $ShortUrl = makeashorterlink($url, "perltweets", "R_3b5bb1d99295e5cc51144bd42c469312");
       print "Shortened URL: ";
       print $ShortUrl;
       print "\n";
    }
    if ($code eq "timeline") {
       my $statuses = $nt->friends_timeline;
       for $status ( @$statuses ) {
           print "$status->{time} <$status->{user}{screen_name}> $status->{text}\n";
       }
    }
    if ($code eq "mentions") {
       my $mentions = $nt->mentions;
       for $status ( @$mentions ) {
           print "$status->{time} <$status->{user}{screen_name}> $status->{text}\n";
       }
    }
    if ($code eq "help") {
       print "Please read http://bit.ly/44rVg7.\n";
    }
    cmdline();
}

cmdline();